package be.wavenet.web;

import java.util.ArrayList;
import java.util.Random;

import javax.servlet.http.HttpServlet;

public class Lotto extends HttpServlet 
{
	private static final long serialVersionUID = -1335572423561034170L;
	private static final int MAX = 42;
	private static final int SIZE = 6;
	private Integer result[];

	public Integer[] getResult() 
	{
		return this.result;
	}

	public void draw()
	{
		this.draw(Lotto.MAX, Lotto.SIZE);
	}

	public void draw(int max, int size)
	{
		this.result = new Integer[size];
		boolean numbers[] = new boolean[max];
		Random random = new Random();
		for (int i = 0; i < size; i++) {
			while (true) {
				int number = random.nextInt(max);
				if (!numbers[number]) {
					numbers[number] = true;
					break;
				}
			}
		}
		int j = 0;
		for (int i = 0; i < max; i++) {
			if (numbers[i])
				this.result[j++] = i + 1;
			if (j == this.result.length)
				break;
		}
	}

	public int getWinningCount(Integer numbers[]) 
	{
		return this.getWinningNumbers(numbers).length;
	}

	public Integer[] getWinningNumbers(Integer numbers[]) 
	{
		ArrayList<Integer> r = new ArrayList<Integer>();
		for (int i = 0; i < this.result.length; i++) {
			Integer number = this.result[i];
			if (i == numbers.length)
				break;
			if (number.equals(numbers[i]))
				r.add(number);
		}
		return r.toArray(new Integer[r.size()]);
	}
}

